//
//  TopicViewController.m
//  Molian
//
//  Created by wansong on 3/31/15.
//  Copyright (c) 2015 xiaocan. All rights reserved.
//

#import "TopicViewController.h"

#import "ArticleCell.h"
#import "CommentViewController.h"
#import "ArticleViewController.h"

@interface TopicViewController ()

@property (nonatomic, strong)UITableView *tableView;

@end

@implementation TopicViewController

#pragma mark - lifecycle

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.topic.title;

    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, DeviceWidth, DeviceHeight)style:UITableViewStyleGrouped];
    self.tableView.backgroundColor = RGBA(244, 244, 244, 1);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollsToTop = NO;
    self.tableView.separatorColor = RGBA(244, 244, 244, 1);//分隔条颜色
    self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 20, 0);//偏移量
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, 51, 0);//滚动条偏移量
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 5)];
    self.tableView.tableFooterView = [[UIView alloc] init];//不显示空的cell
    [self.view addSubview:self.tableView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    // 刷新列表显示
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    if ([self isViewLoaded]&&!self.view.window) {
        self.view = nil;
    }
    // Dispose of any resources that can be recreated.
}

#pragma mark - init

- (void)initArticles {
    // 如果没有文章列表就自己加载
    if (!self.articles) {
        self.articles = [[NSArray alloc]init];
        // 加载专题的文章
        AVQuery *articleQuery = [MLArticle query];
        [articleQuery whereKey:@"topic" equalTo:self.topic];
        [articleQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (!error) {
                // 添加文章到指定的专题
                self.articles = [NSArray arrayWithArray:objects];
            }
        }];
    }
}


#pragma mark - table delegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"TopicArticleCell" ;
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if  (cell == nil) {
        cell = [[ArticleCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    
    // 如果文章列表为空则返回空cell
    if (!self.articles) {
        return cell;
    }
    
    MLArticle *article = self.articles[indexPath.row];
    cell.article = article;
    cell.parentViewController = self;
  //  cell.likeArticles = self.likeArticles;
    if ([Utils inspectArticleReadObjectID:article.objectId]) {
        cell.nImageView.hidden = NO;
    }else{
        cell.nImageView.hidden = YES;
    }
    // 异步加载图片
    [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:article.title_img.url]
                    placeholderImage:[UIImage imageWithColor:[UIColor whiteColor]]];
    cell.titleLabel.text = article.title;
    if(article.commentCount<=0 ){
        article.commentCount = 0;
    }

    NSString *likeStr = [NSString stringWithFormat:@"%ld", (long)article.likeCount];
    cell.likeBtn.Title.text = likeStr;
    [cell.likeBtn layoutSubviews];
    
    

    // 设置文章的喜欢的状态
    BOOL isLiked = NO;
    if ([Utils isLoveArticleObjectId:article.objectId]) {
        isLiked = YES;
    }
    if (!isLiked) {
        cell.likeBtn.status = NO;
        [cell.likeBtn Like:NO Alpha:0.6];
    } else {
        cell.likeBtn.status = YES;
        [cell.likeBtn Like:YES Alpha:0.6];
    }

    return  cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleViewController *articleViewController = [[ArticleViewController alloc]init];
    articleViewController.article =self.articles[indexPath.row];
    articleViewController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:articleViewController animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     return [self.articles count];
}

//设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return EqualHeight(344, 608, DeviceWidth-20);
}

@end
